function Stay(collect){
    this.keep = false;
    if (collect && collect.modConf && collect.modConf.stay && collect.modConf.stay.keep !== undefined) {
        this.keep = Boolean(collect.modConf.stay.keep);
    }
    this.timer = null;
    this.reset();
}
/**
 * 每次间隔时间
 * @type {Number}
 */
Stay.SETP = 5000;

/**
 * 停留时间单位
 * @type {Number}
 */
Stay.STAY = 5;

/**
 * id 过期时间
 * @type {Number}
 */
Stay.EXPIRE = 60;

/**
 * ID 缓存字段
 * @type {String}
 */
Stay.STORE_KEY = "__PI_STAY_SID";
var StayPrototype = Stay.prototype;

StayPrototype.name = "stay";
StayPrototype.report = function(){
    var me = this;
    if (this.timer) {
        clearTimeout(this.timer);
    }
    this.timer = setTimeout(function(){
        this.timer = null;
        clientCollect.send("event", null, "stay");
    }, Stay.SETP);

    return {
        "log_type": "event"
        // 会话停留时间 id
        ,"session_id": sessionStore.get(Stay.STORE_KEY)
        // 页面地址
        ,"url": encodeURIComponent(window.location.href)
        // 事件分类为 time_spent
        ,"category": "behavior"
        // 分类行为为 spend
        ,"action": "stay_time"
        ,"label": "time"
        ,"stay_interval": this.setp++ ? this.time : 0
        ,"title": encodeURIComponent(document.title)
        ,"setp": this.setp
    };
}

/**
 * 重置停留模块
 */
StayPrototype.reset = function() {
    if (this.timer) {
        clearTimeout(this.timer);
    }
    this.setp = 0;
    var sid = sessionStore.get(Stay.STORE_KEY);
    if (!sid) {
        sid = rand(20);
        sessionStore.set(Stay.STORE_KEY, sid, this.keep ? -1 : Stay.EXPIRE);
    }
    this.time = Stay.STAY;
}

clientCollect.add(Stay);
